package cn.peach.user.controller;

import cn.peach.user.domain.Logininfo;
import cn.peach.user.query.LogininfoQuery;
import cn.peach.user.service.ILogininfoService;
import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
@Api(tags = "员工接口", description = "店铺接口详细描述")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据", notes = "")
    public Logininfo findById(@PathVariable("id") Long id) {
        return  logininfoService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有",notes = "")
    public List<Logininfo> findAll() {
        return logininfoService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query) {
        return logininfoService.queryList(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "传一个id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            logininfoService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统。。。");
        }
    }

    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "有id就是修改反之就是添加")
    public AjaxResult addAndUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null){
                logininfoService.insert(logininfo);
            }else {
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统404");
        }
    }
}
